
#include <stdio.h>
#include "wm_hal.h"
#include "img_data.h"

void Error_Handler(void);
void fwup_test(void);

// 升级区起始地址在tools/W806/utilities/aft_build_project.sh里修改upd_img_pos的值，需大于run_img_pos + sizeof(W806.img) - 64
// img_data.h是一个以0x08080000为升级区起始地址的固件，将编译生成的W806_ota.img转成了数组，用于演示demo
int main(void)
{
	SystemClock_Config(CPU_CLK_160M);
	printf("enter main\r\n");
	fwup_test();
	while (1)
	{
		printf(".");
		HAL_Delay(1000);
	}
}

void fwup_test(void)
{
	uint32_t total_len = 0, len = 0, offset = 0;
	FWUP_HandleTypeDef hfwup;
	int ret;
	
	total_len = sizeof(img_buf);
	
	HAL_FWUP_Init(&hfwup);
	while (total_len > 0)
	{
		len = (total_len > 4096) ? 4096 : total_len;
		ret = HAL_FWUP_Write(&hfwup, img_buf + offset, len);
		if (ret != HAL_OK)
		{
			printf("fwup failed\r\n");
			return;
		}
		offset += len;
		total_len -= len;
	}
	ret = HAL_FWUP_Get_Status(&hfwup);
	printf("status = %d please reset\r\n", ret);
}

void Error_Handler(void)
{
	while (1)
	{
	}
}

void assert_failed(uint8_t *file, uint32_t line)
{
	printf("Wrong parameters value: file %s on line %d\r\n", file, line);
}